package com.youmu.sichuansfc.dao.impl;

import com.youmu.sichuansfc.dao.FundManagerFundInvestDao;
import com.youmu.sichuansfc.domain.YoumuFundManagerFundInvest;
import com.youmu.sichuansfc.dto.fund.FundManagerFundInvestDto;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;


@Repository
public class FundManagerFundInvestDaoImpl implements FundManagerFundInvestDao  {
    private EntityManager em;
    @PersistenceContext
    public void setEm(EntityManager em) {
        this.em = em;
    }

    @Override
    public List<FundManagerFundInvestDto> findByManagerId(Long fundId) {

        String sql  = "from YoumuFundManagerFundInvest where is_delete = 0 and manager_fund_id = :fundId";

        return  em.createQuery(sql, FundManagerFundInvestDto.class)
                .setParameter("fundId", fundId)
                .getResultList();
    }

    @Override
    public YoumuFundManagerFundInvest upload(YoumuFundManagerFundInvest youmuFundManagerFundInvest) {
        if(youmuFundManagerFundInvest.getId() != null){
            youmuFundManagerFundInvest = em.merge(youmuFundManagerFundInvest);
        }else {
            em.persist(youmuFundManagerFundInvest);
        }
        return  youmuFundManagerFundInvest;
    }
}
